<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer_mod extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}
	/*
	 *插入数据库-----客户信息
	 *@$dao  array(field=>value);
	 *
	 */
	function InsertCustomer($dao)
	{
		if(is_array($dao))
		{
			return $this->db->insert('tms_customer',$dao);
		}
	}
	
	function getCustomerListBygsid($gsid)
	{
		$gsid = intval($gsid);
		$sql = "SELECT * FROM tms_customer WHERE `gsid` = $gsid";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	/**
	 *
	 *第一次添加客户时的跟踪记录
	 *
	 *参数说明：$p[] key = 
	 *
	 */
	 
	public function FirstAddCustomerWithTrack($p)
	{
		return $this->db->insert('tms_task',$p);
	}
	
	public function AddCustomerWithtmsTrack($p)
	{
		return $this->db->insert('tms_track',$p);
	}
	//查询所有的联系人信息
	public function cust_list()
	{		
		$sql = "select * from (select c.name,t.*,a.m_username from tms_customer as c left join tms_track as t on c.id=t.cid left join tms_admin as a on a.m_id=t.uid order by t.date desc) as abcd group by abcd.name order by abcd.id desc";
		$query = $this->db->query($sql);
		$result=$query->result_array();
		foreach($result as $key => $val)
		{
			$arr[$key]['cid']=$val['cid'];
			$arr[$key]['name']=$val['name'];
			$arr[$key]['m_username']=$val['m_username'];
			$arr[$key]['content']=$val['content'];
			$arr[$key]['date']=date("Y-m-d H:i:s",$val['date']);
		}
		return $arr;
	}
	public function cust_list_info($cid)
	{
		$sql="select a.m_username,t.* from tms_track as t left join tms_admin as a on a.m_id=t.uid where cid= '$cid' order by t.date desc";
		$query=$this->db->query($sql);
		$result=$query->result_array();
		foreach($result as $key => $val)
		{
			$arr[$key]['m_username']=$val['m_username'];
			$arr[$key]['content']=$val['content'];
			$arr[$key]['date']=date("Y-m-d H:i:s",$val['date']);
		}
		return $arr;
	}
}
?>